package cn.youwan.market;

import java.util.ArrayList;
import java.util.List;

import cn.youwan.market.adapter.GridViewAdapter;
import cn.youwan.market.adapter.TuijianAdapter;
import cn.youwan.market.entity.AppListBean;
import cn.youwan.market.entity.PromotionListbean.Appbean;
import cn.youwan.market.message.YouwanMessage;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
/*
 * 精选
 * */
//ceshi
public class ChosenActivity extends Activity {
	private List<View> mViews0 = new ArrayList<View>();
	private ArrayList<String> lis;
	private GridView gd_like;
	private GridViewAdapter adapter;
	private ViewPager pager;
	private boolean isLoop = true;
	private int curitem = 0;
	public Handler mHandler;
	private ListView mChosenList;
	protected AppListBean bean;
	public static TuijianAdapter mAdapter;
	
	public List<Appbean> apps = new ArrayList<Appbean>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chosen);
		mHandler=new Handler();
		lis=new ArrayList<String>();
		lis.add("0 ");
		lis.add("1 ");
		lis.add("2 ");
		lis.add("3 ");
		lis.add("4 ");
		gd_like=(GridView)findViewById(R.id.gd_like);
		adapter=new GridViewAdapter(lis, this);
		gd_like.setAdapter(adapter);
        mChosenList = (ListView) findViewById(R.id.list_chosen);

		for (int i = 0; i < 3; i++) {
			TextView tv = new TextView(this);
			switch (i) {
			case 0:
				tv.setBackgroundResource(R.drawable.a1);
				tv.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent intent = new Intent(ChosenActivity.this,
								PagerActivity.class);
						intent.putExtra("URL",
								"http://m.7pingmi.com.cn/#/game/7");
						startActivity(intent);
					}
				});
				mViews0.add(tv);
				break;
			case 1:
				tv.setBackgroundResource(R.drawable.a2);
				tv.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent intent = new Intent(ChosenActivity.this,
								PagerActivity.class);
						intent.putExtra("URL",
								"http://m.7pingmi.com.cn/#/h5");
						startActivity(intent);
					}
				});
				mViews0.add(tv);
				break;
			case 2:
				tv.setBackgroundResource(R.drawable.a3);
				tv.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent intent = new Intent(ChosenActivity.this,
								PagerActivity.class);
						intent.putExtra("URL",
								"http://m.7pingmi.com.cn/#/cp");
						startActivity(intent);
					}
				});
				mViews0.add(tv);
				break;

			default:
				break;
			}
		}



		pager= (ViewPager) findViewById(R.id.headPager);
		RadioGroup rg = (RadioGroup) findViewById(R.id.pagerRgId);
		final RadioButton rb1 = (RadioButton) rg.findViewById(R.id.rb1Id);
		final RadioButton rb2 = (RadioButton) rg.findViewById(R.id.rb2Id);
		final RadioButton rb3 = (RadioButton) rg.findViewById(R.id.rb3Id);
		MyPagerAdapter headAdapter = new MyPagerAdapter(mViews0);
		pager.setAdapter(headAdapter);
		pager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				curitem=position;
				switch (position) {
				case 0:
					rb1.setChecked(true);
					break;
				case 1:
					rb2.setChecked(true);
					break;
				case 2:
					rb3.setChecked(true);
					break;
				default:
					break;
				}

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int state) {
			}
		});


		rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rb1Id:
					pager.setCurrentItem(0);
					break;
				case R.id.rb2Id:
					pager.setCurrentItem(1);
					break;
				case R.id.rb3Id:
					pager.setCurrentItem(2);
					break;
				default:
					break;
				}

			}
		});

		LoopThread mLoopThread = new LoopThread();
		mLoopThread.start();
		getMoreGameRank();
	}

	class MyPagerAdapter extends PagerAdapter {

		private List<View> mViews = new ArrayList<View>();

		public MyPagerAdapter(List<View> mViews) {
			this.mViews = mViews;
		}

		@Override
		public int getCount() {
			return mViews.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		// 销毁某一个页面
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(mViews.get(position));
		}

		// 创建ViewPager的页面
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			// 往ViewPager中添加界面
			container.addView(mViews.get(position));
			// 把当前页面返回
			return mViews.get(position);
		}
	}

	class LoopThread extends Thread {
		@Override
		public void run() {
			int i = 0;
			while (isLoop) {
				try {
					Thread.sleep(3500);
					curitem = i;
					mHandler.post(new Runnable() {
						@Override
						public void run() {
							pager.setCurrentItem(curitem, true);
						}
					});
					i++;
					i = i % 3;
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public void getMoreGameRank() {
		new AsyncTask<Void, Void, Void>() {

			protected void onPreExecute() {
			};

			@Override
			protected Void doInBackground(Void... params) {
				YouwanMessage message = new YouwanMessage(ChosenActivity.this);
				bean = message.getAppsList("http://gd.7pingmi.com.cn/game/server/?m=gameInfo",1);
				return null;
			}

			@Override
			protected void onPostExecute(Void result) {
				super.onPostExecute(result);
				apps=bean.getBeans();
				Log.i("load APPS", apps.toString());
				mAdapter=new TuijianAdapter(ChosenActivity.this,apps,mChosenList);
				mChosenList.setAdapter(mAdapter);
			}
		}.execute();
	}
}
